from CrawlService import CrawlCommentService
from HotWordAnalyse import HotWordAnalyse
import os


data_dir = "./data_xiecheng"
word_amount = 120 # 词云的词数
crawl_pages = 3 # 爬取页数 -1:爬取所有


"""
主程序（如果需要单独调试，注释部分代码即可）
"""
if __name__ == "__main__":

    """
    1. 爬虫
    """
    spider = CrawlCommentService(data_dir)
    spider.crawl(crawl_pages)


    """
    2. 绘制词云
    """
    analyser = HotWordAnalyse()
    for city_name in os.listdir(data_dir):
        targetFile = f"{data_dir}/{city_name}/comment.txt"
        content = ""
        with open(targetFile,mode="r",encoding="utf-8") as f:
            content = f.read()
        # 得到词频统计
        print(f"正在对{city_name}评论数据进行分析...")
        word_counter = analyser.analyse(content)
        print(f"正在绘制{city_name}的词云图")
        # 绘制词云图
        analyser.draw_cloud(dict(word_counter.most_common(word_amount)),city_name)
        print("绘制完毕")

